package com.nd.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.*;
import com.nd.pojo.bean.*;
import com.nd.pojo.bean.System;
import com.nd.pojo.vo.wx.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class HomeIndexServiceImpl implements HomeIndexService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    AdMapper adMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    SystemMapper systemMapper;

    @Override

    public IndexVo queryIndexVo() {
        //查询小程序的配置
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        HashMap<String, String> config = new HashMap<>();
        for (System system : systems) {
            config.put(system.getKeyName(),system.getKeyValue());
        }
        int catlog_goods = Integer.parseInt(config.get("cskaoyan_mall_wx_catlog_goods"));
        int catlog_list = Integer.parseInt(config.get("cskaoyan_mall_wx_catlog_list"));
        int index_brand = Integer.parseInt(config.get("cskaoyan_mall_wx_index_brand"));
        int index_hot = Integer.parseInt(config.get("cskaoyan_mall_wx_index_hot"));
        int index_new = Integer.parseInt(config.get("cskaoyan_mall_wx_index_new"));
        int index_topic = Integer.parseInt(config.get("cskaoyan_mall_wx_index_topic"));

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        CouponExample couponExample = new CouponExample();
        couponExample.createCriteria().andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        AdExample adExample = new AdExample();
        adExample.createCriteria().andDeletedEqualTo(false);
        List<Ad> ads = adMapper.selectByExample(adExample);
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        TopicExample topicExample = new TopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.createCriteria().andDeletedEqualTo(false);
        List<GrouponRules> grouponRulesList = grouponRulesMapper.selectByExample(grouponRulesExample);
        List<Goods> hotGoodsList = new ArrayList<>();
        List<Goods> newGoodsList = new ArrayList<>();
        List<FloorGoodsVo> floorGoodsVos = new ArrayList<>();
        List<ChannelVo> channelVos = new ArrayList<>();
        for (Goods goods : goodsList) {
            if (goods.getIsHot()) {
                hotGoodsList.add(goods);
            }
            if (goods.getIsNew()) {
                newGoodsList.add(goods);
            }

        }
        for (Category category : categories) {
            if (category.getPid() == 0) {
                CategoryExample categoryExample1 = new CategoryExample();
                categoryExample1.createCriteria().andPidEqualTo(category.getId()).andDeletedEqualTo(false);
                List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
                List<Goods> goodsListF = new ArrayList<Goods>();
                for (Category category1 : categories1) {
                    GoodsExample goodsExample1 = new GoodsExample();
                    goodsExample1.createCriteria().andCategoryIdEqualTo(category1.getId()).andDeletedEqualTo(false);
                    List<Goods> catGoods = goodsMapper.selectByExample(goodsExample1);
                    goodsListF.addAll(catGoods);
                }

                if(goodsListF.size()>4) goodsListF = goodsListF.subList(0,4);
                floorGoodsVos.add(new FloorGoodsVo(category.getId(), category.getName(), goodsListF));

                channelVos.add(new ChannelVo(category.getId(), category.getName(), category.getIconUrl()));
            }
        }
        List<GroupOnListVo> groupOnListVos = new ArrayList<>();
        for (GrouponRules grouponRules : grouponRulesList) {
            Goods goods = goodsMapper.selectByPrimaryKey(grouponRules.getGoodsId());
            BigDecimal grouponprice = goods.getCounterPrice().subtract(grouponRules.getDiscount());
            groupOnListVos.add(new GroupOnListVo(grouponprice, goods, grouponRules.getDiscountMember()));
        }
        if(newGoodsList.size()>index_new) newGoodsList = newGoodsList.subList(0,index_new);
        if(coupons.size()>4) coupons = coupons.subList(0,4);
//        if(channelVos.size()>6) channelVos = channelVos.subList(0,6);
        if(groupOnListVos.size()>6) groupOnListVos = groupOnListVos.subList(0,6);
        if(ads.size()>6) ads = ads.subList(0,6);
        if(brands.size()>index_brand) brands = brands.subList(0,index_brand);
        if(hotGoodsList.size()>index_hot) hotGoodsList = hotGoodsList.subList(0,index_hot);
        if(topics.size()>index_topic) topics = topics.subList(0,index_topic);
        if(floorGoodsVos.size()>6) floorGoodsVos = floorGoodsVos.subList(0,6);

        return new IndexVo(newGoodsList, coupons, channelVos, groupOnListVos, ads, brands, hotGoodsList, topics, floorGoodsVos);
    }

    @Override
    public int queryGoodsCount() {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false);
        long count = goodsMapper.countByExample(goodsExample);
        return (int) count;
    }

    @Override
    public CatalogCurrentVo queryCatalogCurVo(Integer catId) {
        Category category = categoryMapper.selectByPrimaryKey(catId);
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false).andPidEqualTo(category.getId());
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        return new CatalogCurrentVo(category,categories);
    }

    @Override
    public CatalogIndexVo queryCatalogIndexVo() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false).andPidEqualTo(0);
        List<Category> pCategorys = categoryMapper.selectByExample(categoryExample);
        CategoryExample categoryExampleSub = new CategoryExample();
        categoryExampleSub.createCriteria().andDeletedEqualTo(false).andPidEqualTo(pCategorys.get(0).getId());
        List<Category> subCategories = categoryMapper.selectByExample(categoryExampleSub);
        return new CatalogIndexVo(pCategorys.get(0),pCategorys,subCategories);
    }

    @Override
    public WxGrouponListVo queryGrouponList(Integer page, Integer size) {
        Date now = new Date();
        PageHelper.startPage(page,size);
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause("discount"+" "+"desc");
        grouponRulesExample.createCriteria().andDeletedEqualTo(false).andExpireTimeLessThan(now);
        List<GrouponRules> grouponRulesList = grouponRulesMapper.selectByExample(grouponRulesExample);
        List<WxGroupVo> wxGroupVos = new ArrayList<>();
        for (GrouponRules grouponRules : grouponRulesList) {
            Integer goodsId = grouponRules.getGoodsId();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            BigDecimal grouponPrice = goods.getRetailPrice().subtract(grouponRules.getDiscount());
            wxGroupVos.add(new WxGroupVo(goods,grouponRules.getDiscountMember(),grouponPrice));
        }
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>();
        long count = grouponRulesPageInfo.getTotal();

        return new WxGrouponListVo(count,wxGroupVos);
    }

}
